package com.cbn.ibcp.jishiting.statistic.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbn.ibcp.common.annotation.DataSource;
import com.cbn.ibcp.common.enums.DataSourceType;
import com.cbn.ibcp.jishiting.gs.cms.service.ICpVideoService;
import com.cbn.ibcp.jishiting.rms.enums.ResBaseOssInjectStatusEnum;
import com.cbn.ibcp.jishiting.rms.service.IResBaseService;
import com.cbn.ibcp.jishiting.statistic.domain.OssInjectDataStatistic;
import com.cbn.ibcp.jishiting.statistic.enums.DateTypeEnum;
import com.cbn.ibcp.jishiting.statistic.enums.DeleteStatusEnums;
import com.cbn.ibcp.jishiting.statistic.mapper.OssInjectDataStatisticMapper;
import com.cbn.ibcp.jishiting.statistic.service.IOssInjectDataStatisticService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class OssInjectDataStatisticServiceImpl implements IOssInjectDataStatisticService {

    private final OssInjectDataStatisticMapper ossInjectDataStatisticMapper;

    private final IResBaseService resBaseService;

    private final ICpVideoService cpVideoService;


    @DataSource(DataSourceType.jishiting_statistic)
    @Override
    public List<OssInjectDataStatistic> selectList(Date startTime, Date endTime) {
        LambdaQueryWrapper<OssInjectDataStatistic> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(startTime)) {
            queryWrapper.ge(OssInjectDataStatistic::getCreateTime, startTime);
        }
        if (Objects.nonNull(endTime)) {
            queryWrapper.le(OssInjectDataStatistic::getCreateTime, endTime);
        }
        queryWrapper.eq(OssInjectDataStatistic::getDeleteStatus, DeleteStatusEnums.DELETED_STATUS_0.getCode());
        queryWrapper.orderByAsc(OssInjectDataStatistic::getCreateTime);
        return ossInjectDataStatisticMapper.selectList(queryWrapper);
    }

    @DataSource(DataSourceType.jishiting_statistic)
    @Override
    public int insert(OssInjectDataStatistic ossInjectDataStatistic) {
        if (Objects.isNull(ossInjectDataStatistic)) {
            return 0;
        }
        return ossInjectDataStatisticMapper.insert(ossInjectDataStatistic);

    }

    @Override
    public OssInjectDataStatistic getOssInjectDataStatistic(Date startTime, Date endTime) {
        //rms创建时间统计
        Integer rmsAllTotalCreateInject = resBaseService.getTotalCreateInjectStatistic(null, null, null, null);
        Integer rmsAllTotalCreateInjecting = resBaseService.getTotalCreateInjectStatistic(null, null, null, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer rmsAllTotalCreateInjectSuccess = resBaseService.getTotalCreateInjectStatistic(null, null, null, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer rmsAllTotalCreateInjectFailure = resBaseService.getTotalCreateInjectFailureStatistic(null, null, null);
        //rms 更新时间统计
        Integer rmsAllTotalUpdateInject = resBaseService.getTotalUpdateInjectStatistic(null, null, null, null);
        Integer rmsAllTotalUpdateInjecting = resBaseService.getTotalUpdateInjectStatistic(null, null, null, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer rmsAllTotalUpdateInjectSuccess = resBaseService.getTotalUpdateInjectStatistic(null, null, null, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer rmsAllTotalUpdateInjectFailure = resBaseService.getTotalUpdateInjectFailureStatistic(null, null, null);
        //rms 时间段创建统计
        Integer rmsTotalCreateInject = resBaseService.getTotalCreateInjectStatistic(null, startTime, endTime, null);
        Integer rmsTotalCreateInjecting = resBaseService.getTotalCreateInjectStatistic(null, startTime, endTime, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer rmsTotalCreateInjectSuccess = resBaseService.getTotalCreateInjectStatistic(null, startTime, endTime, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer rmsTotalCreateInjectFailure = resBaseService.getTotalCreateInjectFailureStatistic(null, startTime, endTime);
        //rms 时间段更新统计
        Integer rmsTotalUpdateInject = resBaseService.getTotalUpdateInjectStatistic(null, startTime, endTime, null);
        Integer rmsTotalUpdateInjecting = resBaseService.getTotalUpdateInjectStatistic(null, startTime, endTime, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer rmsTotalUpdateInjectSuccess = resBaseService.getTotalUpdateInjectStatistic(null, startTime, endTime, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer rmsTotalUpdateInjectFailure = resBaseService.getTotalUpdateInjectFailureStatistic(null, startTime, endTime);
        //cdn 创建时间统计
        Integer cdnAllTotalCreateInject = cpVideoService.getTotalCreateInjectStatistic(null, null, null);
        Integer cdnAllTotalCreateInjecting = cpVideoService.getTotalCreateInjectStatistic(null, null, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer cdnAllTotalCreateInjectSuccess = cpVideoService.getTotalCreateInjectStatistic(null, null, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer cdnAllTotalCreateInjectFailure = cpVideoService.getTotalCreateInjectFailureStatistic(null, null);
        //cdn 更新时间统计
        Integer cdnAllTotalUpdateInject = cpVideoService.getTotalUpdateInjectStatistic(null, null, null);
        Integer cdnAllTotalUpdateInjecting = cpVideoService.getTotalUpdateInjectStatistic(null, null, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer cdnAllTotalUpdateInjectSuccess = cpVideoService.getTotalUpdateInjectStatistic(null, null, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer cdnAllTotalUpdateInjectFailure = cpVideoService.getTotalUpdateInjectFailureStatistic(null, null);
        // cdn 时间段创建统计
        Integer cdnTotalCreateInject = cpVideoService.getTotalCreateInjectStatistic(startTime, endTime, null);
        Integer cdnTotalCreateInjecting = cpVideoService.getTotalCreateInjectStatistic(startTime, endTime, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer cdnTotalCreateInjectSuccess = cpVideoService.getTotalCreateInjectStatistic(startTime, endTime, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer cdnTotalCreateInjectFailure = cpVideoService.getTotalCreateInjectFailureStatistic(startTime, endTime);
        // cdn 时间段更新统计
        Integer cdnTotalUpdateInject = cpVideoService.getTotalUpdateInjectStatistic(startTime, endTime, null);
        Integer cdnTotalUpdateInjecting = cpVideoService.getTotalUpdateInjectStatistic(startTime, endTime, ResBaseOssInjectStatusEnum.INJECTING.getCode());
        Integer cdnTotalUpdateInjectSuccess = cpVideoService.getTotalUpdateInjectStatistic(startTime, endTime, ResBaseOssInjectStatusEnum.INJECT_SUCCESS.getCode());
        Integer cdnTotalUpdateInjectFailure = cpVideoService.getTotalUpdateInjectFailureStatistic(startTime, endTime);
        //新增记录
        OssInjectDataStatistic ossInjectDataStatistic = new OssInjectDataStatistic();
        ossInjectDataStatistic.setDateId(DateUtil.format(endTime, "yyyyMMddHHmm"));
        ossInjectDataStatistic.setDateType(DateTypeEnum.MINUTE_DATE_TYPE_5_MINUTE.getValue());
        //rms创建时间统计
        ossInjectDataStatistic.setRmsAllTotalCreateInject(rmsAllTotalCreateInject.longValue());
        ossInjectDataStatistic.setRmsAllTotalCreateInjecting(rmsAllTotalCreateInjecting.longValue());
        ossInjectDataStatistic.setRmsAllTotalCreateInjectSuccess(rmsAllTotalCreateInjectSuccess.longValue());
        ossInjectDataStatistic.setRmsAllTotalCreateInjectFailure(rmsAllTotalCreateInjectFailure.longValue());
        //rms更新时间统计
        ossInjectDataStatistic.setRmsAllTotalUpdateInject(rmsAllTotalUpdateInject.longValue());
        ossInjectDataStatistic.setRmsAllTotalUpdateInjecting(rmsAllTotalUpdateInjecting.longValue());
        ossInjectDataStatistic.setRmsAllTotalUpdateInjectSuccess(rmsAllTotalUpdateInjectSuccess.longValue());
        ossInjectDataStatistic.setRmsAllTotalUpdateInjectFailure(rmsAllTotalUpdateInjectFailure.longValue());
        //rms 时间段创建数据统计
        ossInjectDataStatistic.setRmsTotalCreateInject(rmsTotalCreateInject.longValue());
        ossInjectDataStatistic.setRmsTotalCreateInjecting(rmsTotalCreateInjecting.longValue());
        ossInjectDataStatistic.setRmsTotalCreateInjectSuccess(rmsTotalCreateInjectSuccess.longValue());
        ossInjectDataStatistic.setRmsTotalCreateInjectFailure(rmsTotalCreateInjectFailure.longValue());
        //rms 时间段更新数据统计
        ossInjectDataStatistic.setRmsTotalUpdateInject(rmsTotalUpdateInject.longValue());
        ossInjectDataStatistic.setRmsTotalUpdateInjecting(rmsTotalUpdateInjecting.longValue());
        ossInjectDataStatistic.setRmsTotalUpdateInjectSuccess(rmsTotalUpdateInjectSuccess.longValue());
        ossInjectDataStatistic.setRmsTotalUpdateInjectFailure(rmsTotalUpdateInjectFailure.longValue());
        ossInjectDataStatistic.setCreateTime(new Date());
        //cdn创建时间统计
        ossInjectDataStatistic.setCdnAllTotalCreateInject(cdnAllTotalCreateInject.longValue());
        ossInjectDataStatistic.setCdnAllTotalCreateInjecting(cdnAllTotalCreateInjecting.longValue());
        ossInjectDataStatistic.setCdnAllTotalCreateInjectSuccess(cdnAllTotalCreateInjectSuccess.longValue());
        ossInjectDataStatistic.setCdnAllTotalCreateInjectFailure(cdnAllTotalCreateInjectFailure.longValue());
        //cdn更新时间统计
        ossInjectDataStatistic.setCdnAllTotalUpdateInject(cdnAllTotalUpdateInject.longValue());
        ossInjectDataStatistic.setCdnAllTotalUpdateInjecting(cdnAllTotalUpdateInjecting.longValue());
        ossInjectDataStatistic.setCdnAllTotalUpdateInjectSuccess(cdnAllTotalUpdateInjectSuccess.longValue());
        ossInjectDataStatistic.setCdnAllTotalUpdateInjectFailure(cdnAllTotalUpdateInjectFailure.longValue());
        // cdn 时间段创建时间统计
        ossInjectDataStatistic.setCdnTotalCreateInject(cdnTotalCreateInject.longValue());
        ossInjectDataStatistic.setCdnTotalCreateInjecting(cdnTotalCreateInjecting.longValue());
        ossInjectDataStatistic.setCdnTotalCreateInjectSuccess(cdnTotalCreateInjectSuccess.longValue());
        ossInjectDataStatistic.setCdnTotalCreateInjectFailure(cdnTotalCreateInjectFailure.longValue());
        // cdn 时间段更新时间统计
        ossInjectDataStatistic.setCdnTotalUpdateInject(cdnTotalUpdateInject.longValue());
        ossInjectDataStatistic.setCdnTotalUpdateInjecting(cdnTotalUpdateInjecting.longValue());
        ossInjectDataStatistic.setCdnTotalUpdateInjectSuccess(cdnTotalUpdateInjectSuccess.longValue());
        ossInjectDataStatistic.setCdnTotalUpdateInjectFailure(cdnTotalUpdateInjectFailure.longValue());
        return ossInjectDataStatistic;
    }
}
